//
// Created by huafv on 8/20/24.
//

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/ide.h>
#include <linux/cdev.h>
#include <linux/device.h>

#include <asm/mach/map.h>

#define LEDOFF 	0				/* led off */
#define LEDON 	1				/* led on */

// driver raw reg
#define CCM_CCGR1_BASE				(0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE		(0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE		(0X020E02F4)
#define GPIO1_DR_BASE				(0X0209C000)
#define GPIO1_GDIR_BASE				(0X0209C004)


// driver io mem
static void __iomem *CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

#define NEWCHRLED_CNT			1
#define NEWCHRLED_NAME "newchrled"

static int led_open (struct inode *inode, struct file *filp);
static ssize_t led_read (struct file *filp, char __user *buf, size_t cnt, loff_t *offt);
static ssize_t led_write (struct file *filp, const char __user *buf, size_t cnt, loff_t *offt);
static int led_release(struct inode *inode, struct file *filp);

struct newchrled_dev {
    dev_t dev_id;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
};

static struct file_operations newchrled_fops = {
    .owner = THIS_MODULE,
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release
};

struct newchrled_dev newchrled;

static int __init newchrled_init(void) {
    int ret = 0;
    // init led
    u32 val;

    /* 1、hw_reg to io_mem */
    CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
    SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
    SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
    GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
    GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

    /* 2、enable GPIO1 clock */
    val = readl(CCM_CCGR1);
    val &= ~(3 << 26);	/* clear old setting */
    val |= (3 << 26);	/* set newval */
    writel(val, CCM_CCGR1);

    /* 3、set GPIO1_IO03 mux，mux set
     *    GPIO1_IO03，set io attr。
     */
    writel(5, SW_MUX_GPIO1_IO03);

    /*reg SW_PAD_GPIO1_IO03 set io attr
     *bit 16:0 HYS关闭
     *bit [15:14]: 00 默认下拉
     *bit [13]: 0 kepper功能
     *bit [12]: 1 pull/keeper使能
     *bit [11]: 0 关闭开路输出
     *bit [7:6]: 10 速度100Mhz
     *bit [5:3]: 110 R0/6驱动能力
     *bit [0]: 0 低转换率
     */
    writel(0x10B0, SW_PAD_GPIO1_IO03);

    /* 4、set GPIO1_IO03 output */
    val = readl(GPIO1_GDIR);
    val &= ~(1 << 3);	/* clear old setting */
    val |= (1 << 3);	/* set output */
    writel(val, GPIO1_GDIR);

    /* 5、default led off  */
    val = readl(GPIO1_DR);
    val |= (1 << 3);
    writel(val, GPIO1_DR);
    // register chrdev
    if(newchrled.major) {
        newchrled.dev_id = MKDEV(newchrled.major, 0);
        ret = register_chrdev_region(newchrled.dev_id, NEWCHRLED_CNT, NEWCHRLED_NAME);
    }else {
        ret = alloc_chrdev_region(&newchrled.dev_id, 0, NEWCHRLED_CNT, NEWCHRLED_NAME);
        newchrled.major = MAJOR(newchrled.dev_id);
        newchrled.minor = MINOR(newchrled.dev_id);
    }
    if(ret < 0) {
        printk(KERN_ALERT "Failed to register chrled\n");
        return ret;
    }
    printk(KERN_INFO "chrled device registered\n");
    printk(KERN_INFO "Major: %d\n", newchrled.major);
    printk(KERN_INFO "Minor: %d\n", newchrled.minor);
    // add chr
    newchrled.cdev.owner 		= THIS_MODULE;
    cdev_init(&newchrled.cdev, &newchrled_fops);
    // newchrled.cdev.ops = &newchrled_fops;
    cdev_add(&newchrled.cdev, newchrled.dev_id, NEWCHRLED_CNT);

    // add class
    newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
    if (IS_ERR(newchrled.class)) {
        return PTR_ERR(newchrled.class);
    }

    /* create device */
    newchrled.device = device_create(newchrled.class, NULL, newchrled.dev_id, NULL, NEWCHRLED_NAME);
    if (IS_ERR(newchrled.device)) {
        return PTR_ERR(newchrled.device);
    }

    return 0;
}
static void __exit newchrled_exit(void) {
    /* rm iomem */
    iounmap(CCM_CCGR1);
    iounmap(SW_MUX_GPIO1_IO03);
    iounmap(SW_PAD_GPIO1_IO03);
    iounmap(GPIO1_DR);
    iounmap(GPIO1_GDIR);

    /* unregister_chrdev_region */
    cdev_del(&newchrled.cdev);/*  del cdev */
    unregister_chrdev_region(newchrled.dev_id, NEWCHRLED_CNT); /* unchrdev */

    device_destroy(newchrled.class, newchrled.dev_id);
    class_destroy(newchrled.class);
}

/*
 * @description		: LED打开/关闭
 * @param - sta 	: LEDON(0) 打开LED，LEDOFF(1) 关闭LED
 * @return 			: 无
 */
void led_switch(u8 sta)
{
    u32 val = 0;
    if(sta == LEDON) {
        val = readl(GPIO1_DR);
        val &= ~(1 << 3);
        writel(val, GPIO1_DR);
    }else if(sta == LEDOFF) {
        val = readl(GPIO1_DR);
        val|= (1 << 3);
        writel(val, GPIO1_DR);
    }
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件，file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int led_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &newchrled; /* 设置私有数据 */
    return 0;
}

/*
 * @description		: 从设备读取数据
 * @param - filp 	: 要打开的设备文件(文件描述符)
 * @param - buf 	: 返回给用户空间的数据缓冲区
 * @param - cnt 	: 要读取的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 读取的字节数，如果为负值，表示读取失败
 */
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    return 0;
}

/*
 * @description		: 向设备写数据
 * @param - filp 	: 设备文件，表示打开的文件描述符
 * @param - buf 	: 要写给设备写入的数据
 * @param - cnt 	: 要写入的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 写入的字节数，如果为负值，表示写入失败
 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[1];
    unsigned char ledstat;

    retvalue = copy_from_user(databuf, buf, cnt);
    if(retvalue < 0) {
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }

    ledstat = databuf[0];		/* 获取状态值 */

    if(ledstat == LEDON) {
        led_switch(LEDON);		/* 打开LED灯 */
    } else if(ledstat == LEDOFF) {
        led_switch(LEDOFF);	/* 关闭LED灯 */
    }
    return 0;
}

/*
 * @description		: 关闭/释放设备
 * @param - filp 	: 要关闭的设备文件(文件描述符)
 * @return 			: 0 成功;其他 失败
 */
static int led_release(struct inode *inode, struct file *filp)
{
    return 0;
}

/* register driver an unregister driver */
module_init(newchrled_init);
module_exit(newchrled_exit);

/* license */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("huafv");